package com.lg.partition21;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/30 9:46
 */
public class P2014选课 {

    private static final int N = 310, M = 2 * N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[N];
    private static int[][] f = new int[N][N];
    private static int n, m, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split("\\s+");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            ss = reader.readLine().split("\\s+");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            add(a, i);
            add(i, a);
            w[i] = b;
        }
        dfs(0, -1);
        writer.write(f[0][m] + "\n");
        writer.flush();
    }

    private static void dfs(int u, int parent) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == parent) continue;
            dfs(v, u);
            for (int j = m; j >= 0; j--) {
                for (int k = 0; k <= j; k++) {
                    f[u][j] = Math.max(f[u][j], f[u][j - k] + f[v][k]);
                }
            }
        }
        if (u != 0) {
            for (int i = m; i >= 1; i--) {
                f[u][i] = f[u][i - 1] + w[u];
            }
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
